package cn.yfxu.server.exception;

import cn.yfxu.server.pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
* @author Xxxxyf
* @date 2021/3/9
* @desc 全局异常处理
*/
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public Result mysqlException(SQLException e){
        if (e instanceof SQLIntegrityConstraintViolationException){
            return Result.error("该数据有关联数据，操作失败");
        }
        return Result.error("数据库异常，操作失败！");
    }

    @ExceptionHandler(Exception.class)
    public Result Exception(Exception e){
        return Result.error(e.getMessage());
    }
}
